package com.bjypc.youth.web.json;

import org.apache.log4j.Logger;

import com.bjypc.youth.entity.InterpersonalRelationship;
import com.bjypc.youth.entity.LifeStyle;
import com.bjypc.youth.entity.Student;
import com.bjypc.youth.entity.StudentAssociation;
import com.bjypc.youth.entity.StudentConsume;
import com.bjypc.youth.entity.StudentFeeling;
import com.bjypc.youth.entity.StudentFutureJudgment;
import com.bjypc.youth.entity.StudentImportentLevel;
import com.bjypc.youth.entity.StudentInterest;
import com.bjypc.youth.entity.TimeUsing;
import com.bjypc.youth.service.IStudentService;
import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class StudentTestAction extends ActionSupport {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 8705273808015920398L;
	/**
	 * Entities that will be persisted
	 */
	private Student person;
	private LifeStyle lifeStyle;
	private InterpersonalRelationship relationship;
	private StudentAssociation studentAssociation;
	private TimeUsing timeUsing;
	private StudentFeeling personalFeeling;
	private StudentInterest interest;
	private StudentImportentLevel importentLevel;
	private StudentFutureJudgment futureJudgment;
	private StudentConsume personConsume;
	private IStudentService studentService;

	/**
	 * status field to indicate whether operation succeeds
	 */
	private boolean success;
	private boolean failure;

	public String execute() {
		if (null != person && person.getId() == null) {

			// 保存生活方式
			if (null != lifeStyle) {
				person.setLifeStyle(lifeStyle);
			}
			// 保存与人交往
			if (null != relationship) {
				person.setInterpersonalRelationship(relationship);
			}
			// 保存与人交往（二）
			if (null != studentAssociation) {
				person.setStudentAssociation(studentAssociation);
			}
			// 保存学生兴趣
			if (null != interest) {
				person.setPersonalInterest(interest);
			}
			// 保存学生消费
			if (null != this.personConsume) {
				person.setPersonConsume(personConsume);
			}
			// add TimeUsing
			if (null != timeUsing) {
				person.setTimeUsing(timeUsing);
			}
			// 保存学生感受
			if (null != personalFeeling) {
				person.setPersonalFeeling(personalFeeling);
			}
			// 保存学生重要性
			if (null != this.importentLevel) {
				person.setImportentLevel(importentLevel);
			}
			// 保存学生未来判断
			if (null != this.futureJudgment) {
				person.setFutureJudgment(futureJudgment);
			}

			this.studentService.save(person);
			logger.info("Person with sex " + person.getSex()
					+ " has been persisted,id is " + this.person.getId());
			this.success = true;
			this.failure = false;
		} else {
			if (null == person)
				logger.debug("Person is null,can't be saved to DB");
			else if (null != person.getId())
				logger.debug("Person with existed id(" + person.getId()
						+ ") can't be saved to DB");

		}
		return Action.SUCCESS;
	}

	protected Logger logger = Logger.getLogger(StudentTestAction.class);

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public boolean isFailure() {
		return failure;
	}

	public void setFailure(boolean failure) {
		this.failure = failure;
	}

	@JSON(serialize = false)
	public LifeStyle getLifeStyle() {
		return lifeStyle;
	}

	public void setLifeStyle(LifeStyle lifeStyle) {
		this.lifeStyle = lifeStyle;
	}

	@JSON(serialize = false)
	public InterpersonalRelationship getRelationship() {
		return relationship;
	}

	public void setRelationship(InterpersonalRelationship relationship) {
		this.relationship = relationship;
	}

	@JSON(serialize = false)
	public StudentInterest getInterest() {
		return interest;
	}

	public void setInterest(StudentInterest interest) {
		this.interest = interest;
	}

	@JSON(serialize = false)
	public TimeUsing getTimeUsing() {
		return timeUsing;
	}

	public void setTimeUsing(TimeUsing timeUsing) {
		this.timeUsing = timeUsing;
	}

	@JSON(serialize = false)
	public StudentFeeling getPersonalFeeling() {
		return personalFeeling;
	}

	public void setPersonalFeeling(StudentFeeling personalFeeling) {
		this.personalFeeling = personalFeeling;
	}

	@JSON(serialize = false)
	public StudentImportentLevel getImportentLevel() {
		return importentLevel;
	}

	public void setImportentLevel(StudentImportentLevel importentLevel) {
		this.importentLevel = importentLevel;
	}

	@JSON(serialize = false)
	public StudentFutureJudgment getFutureJudgment() {
		return futureJudgment;
	}

	public void setFutureJudgment(StudentFutureJudgment futureJudgment) {
		this.futureJudgment = futureJudgment;
	}

	@JSON(serialize = false)
	public StudentConsume getPersonConsume() {
		return personConsume;
	}

	public void setPersonConsume(StudentConsume personConsume) {
		this.personConsume = personConsume;
	}

	@JSON(serialize = false)
	public Student getPerson() {
		return person;
	}

	public void setPerson(Student person) {
		this.person = person;
	}

	@JSON(serialize = false, deserialize = false)
	public IStudentService getStudentService() {
		return studentService;
	}

	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}

	@JSON(serialize = false)
	public StudentAssociation getStudentAssociation() {
		return studentAssociation;
	}

	public void setStudentAssociation(StudentAssociation studentAssociation) {
		this.studentAssociation = studentAssociation;
	}

}
